package com.wwwarehouse.resource_center.adapter.salesInventory_statistics;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;

import com.wwwarehouse.resource_center.bean.sales_inventory_statistics.CustomerSupplierResponseBean;

import java.util.ArrayList;
import java.util.List;

/**
 * 多选筛选器的Adapter
 * Created by ShineF on 2017/7/11 0011.
 */

public class FilterMultipleDoubleAdapter extends BaseAdapter {
    private Context mContext;
    private CustomerSupplierResponseBean mList;
    private List<CustomerSupplierResponseBean.SupplierBusBean> supplierBusBeanList;
    private ViewHolder mHolder;
    private boolean isSupplier;//是否是供应商
    private OnClickListener onClickListener;

    private List<CustomerSupplierResponseBean.SupplierBusBean> mSelectedSupplier;
    private List<CustomerSupplierResponseBean.CustomerBusBean> mSelectedCustomer;

    public FilterMultipleDoubleAdapter(Context context, CustomerSupplierResponseBean mFilterList, List<CustomerSupplierResponseBean.SupplierBusBean> supplierBusBeanList, boolean isSupplier, OnClickListener onClickListener) {
        this.mList = mFilterList;
        this.mContext = context;
        this.supplierBusBeanList = supplierBusBeanList;
        this.isSupplier = isSupplier;
        this.onClickListener = onClickListener;
        mSelectedSupplier = new ArrayList<>();
        mSelectedCustomer = new ArrayList<>();
    }

    @Override
    public int getCount() {
        if (isSupplier)
            return mList.getSupplierBus().size();
        else
            return mList.getCustomerBus().size();

    }

    @Override
    public Object getItem(int position) {
        if (isSupplier)
            return mList.getSupplierBus().get(position);
        else
            return mList.getCustomerBus().get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(mContext, com.wwwarehouse.common.R.layout.item_drawer_filter_multiple, null);
            mHolder = new ViewHolder(convertView);
            convertView.setTag(mHolder);
        } else {
            mHolder = (ViewHolder) convertView.getTag();
        }

        if (isSupplier) {
            final CustomerSupplierResponseBean.SupplierBusBean supplierBusBean = mList.getSupplierBus().get(position);
            if (mSelectedSupplier.size() == 0) {
                mHolder.mBtn.setSelected(false);
                mHolder.mBtn.setVisibility(View.VISIBLE);
            } else {
                if (isSelected(supplierBusBean.getSupplierBuId())) {
                    mHolder.mBtn.setVisibility(View.VISIBLE);
                    mHolder.mBtn.setSelected(true);
                } else {
                    mHolder.mBtn.setVisibility(View.GONE);
                }
            }
            String supplierBuName = supplierBusBean.getSupplierBuName();
            mHolder.mBtn.setText(supplierBuName);
            mHolder.mBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (isSelected(supplierBusBean.getSupplierBuId())) {
                        mSelectedSupplier.clear();
                    } else {
                        mSelectedSupplier.clear();
                        mSelectedSupplier.add(supplierBusBean);
                    }
                    onClickListener.onSupplierClickListener();

                }
            });
        } else {
            final CustomerSupplierResponseBean.CustomerBusBean customerBusBean = mList.getCustomerBus().get(position);
            if (isSelected(customerBusBean.getCustomerBuId())) {
                mHolder.mBtn.setSelected(true);
            } else {
                mHolder.mBtn.setSelected(false);
            }
            String customerBuName = customerBusBean.getCustomerBuName();
            mHolder.mBtn.setText(customerBuName);
            mHolder.mBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    changeSelected(customerBusBean);
                    onClickListener.onCustomerClickListener();
                }
            });
        }
        return convertView;
    }


    class ViewHolder {
        Button mBtn;

        public ViewHolder(View view) {
            mBtn = (Button) view.findViewById(com.wwwarehouse.common.R.id.btn_item);
        }
    }

    public interface OnClickListener {
        void onSupplierClickListener();

        void onCustomerClickListener();
    }

    public void setmList(CustomerSupplierResponseBean mList) {
        this.mList = mList;
    }

    public List<CustomerSupplierResponseBean.SupplierBusBean> getmSelectedSupplier() {
        return mSelectedSupplier;
    }

    public void setmSelectedSupplier(List<CustomerSupplierResponseBean.SupplierBusBean> mSelectedSupplier) {
        this.mSelectedSupplier = mSelectedSupplier;
    }

    public List<CustomerSupplierResponseBean.CustomerBusBean> getmSelectedCustomer() {
        return mSelectedCustomer;
    }

    public void setmSelectedCustomer(List<CustomerSupplierResponseBean.CustomerBusBean> mSelectedCustomer) {
        this.mSelectedCustomer = mSelectedCustomer;
    }

    private boolean isSelected(String s) {
        if (isSupplier) {
            boolean isContains = false;
            for (CustomerSupplierResponseBean.SupplierBusBean supplierBusBean : mSelectedSupplier) {
                if (supplierBusBean.getSupplierBuId().equals(s))
                    isContains = true;
            }
            return isContains;
        } else {
            boolean isContains = false;
            for (CustomerSupplierResponseBean.CustomerBusBean customerBusBean : mSelectedCustomer) {
                if (customerBusBean.getCustomerBuId().equals(s))
                    isContains = true;
            }
            return isContains;
        }
    }

    private void changeSelected(CustomerSupplierResponseBean.CustomerBusBean selectedBean) {
        CustomerSupplierResponseBean.CustomerBusBean bean = null;
        for (CustomerSupplierResponseBean.CustomerBusBean customerBusBean : mSelectedCustomer) {
            if (customerBusBean.getCustomerBuId().equals(selectedBean.getCustomerBuId())) {
                bean = customerBusBean;
            }
        }
        if (bean != null) {
            mSelectedCustomer.remove(bean);
        } else {
            mSelectedCustomer.add(selectedBean);
        }
    }
}
